#include<iostream>

using namespace std;
const int N=1e4+10;
int n,p;
char result[N][N];
char compare(int x,int y)
{
	if(result[x][y]) return result[x][y];
	if(x==0) return '1';
	if(y==0) return '2';
	result[x][y]='3';
	return result[x][y]=compare((x+y)%p,(x+y+y)%p);
}
int main()
{
	cin>>n>>p;
	int a,b;
	while(n--)
	{
		cin>>a>>b;
		//a->(a+b)%p
		//b->(a+b+b)%p
		compare(a,b);
		if(result[a][b]=='1'||result[a][b]=='2')
		{
			cout<<result[a][b]<<endl;
		}
		else 
		{
			cout<<"error"<<endl;
		}
	}
}